<!-- 编辑弹窗 -->
<template>
    <ele-modal
      width="30%"
      :movable="false"
      :visible="visible"
      :close-on-click-modal="true"
      custom-class="ele-dialog-form"
      :title="isUpdate ? '修改汇率设置信息' : '添加汇率设置信息'"
      @update:visible="updateVisible"
    >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px" >
        <el-card  >        
          <el-row :gutter="24">
            <el-col :md="24" :sm="24" >
              <el-form-item label="货币兑:" prop="scurrencyId">
                <el-select  v-model="form.scurrencyId" filterable clearable placeholder="请选择货币" style="width:100%" >
                  <el-option  v-for="item in HBTypeDatas" :key="item.id" :label="item.name" :value="item.id"/>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :md="24" :sm="24">
              <el-form-item label="货币换:" prop="ecurrencyId">
                <el-select  v-model="form.ecurrencyId" filterable clearable placeholder="请选择货币" style="width:100%" >
                  <el-option  v-for="item in HBTypeDatas" :key="item.id" :label="item.name" :value="item.id"/>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :md="24" :sm="24">
              <el-form-item label="汇率:" prop="rate">
                <el-input v-model="form.rate" placeholder="请输入汇率"   >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :md="24" :sm="24">
              <el-form-item label="运算:" prop="computeDirection">
                <el-select  v-model="form.computeDirection" filterable clearable placeholder="请选择运算" style="width:100%" >
                  <el-option  v-for="(item, index) in computeDirectionDatas" :key="index" :label="item.Value" :value="item.Key"/>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :md="24" :sm="24">
              <el-form-item label="表达式:" >
                <span style="color:red">货币换= 货币兑 {{ form.computeDirection==1?'×':'÷' }} 汇率</span>
              </el-form-item>
            </el-col>
          </el-row>
        </el-card>
    </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button type="primary" v-show="this.btnIndex!=3" :loading="loading" @click="save" v-permission="'Basic_Rate_Save'">
        保存
      </el-button>
    </div>
    </ele-modal>
  </template>
  <script>

    import { isIntegerOrDecimal8 } from '@/utils/validate';
    import { QueryComputeDirectionEnum } from '@/api/enummanger';
    import { QueryAllChildDictViewByCode } from '@/api/systemmanager/dictmanager';
    import { AddRate, UpdateRate,GetRateById } from '@/api/basicdatamanager/ratemanager';
    export default {
      name: 'RateEdit',
      props: {
        // 弹窗是否打开
        visible: Boolean,
        // 修改回显的数据
        data: Object,
        // 按钮索引
        btnIndex:String,
      },
      data() {
        return {
          // 表单数据
          form: {},
          // 表单验证规则
          rules: {
            scurrencyId: [{required: true,message: '请输入货币兑',trigger: 'blur'}],
            ecurrencyId: [{required: true,message: '请输入货币换',trigger: 'blur'}],
            computeDirection: [{required: true,message: '请选择运算',trigger: 'blur'}],
            rate: [{validator:isIntegerOrDecimal8,trigger:'blur'},{required: true,message: '请输入汇率',trigger: 'blur'}],
          },
          // 提交状态
          loading: false,
          // 是否是修改
          isUpdate: false,
          HBTypeDatas:[],
          computeDirectionDatas:[],
        };
      },

      //初始化数据
      mounted(){
        this.initDW();
      },
      methods: {
  
        /* 保存编辑 */
        save() {
          //校验必填信息
          this.$refs['form'].validate((valid) => {
            if (!valid) {return false;}

            this.loading = true;
            const saveOrUpdate = this.isUpdate ? UpdateRate : AddRate;
            saveOrUpdate(this.form)
              .then((msg) => {
                this.loading = false;
                this.$message.success(msg);
                this.updateVisible(false);
                //回调父组件方法
                this.$emit('done');
              })
              .catch((e) => {
                this.loading = false;
                this.$message.error(e.message);
              });
          });
        },

        setData(id){
          GetRateById(id)
            .then((res) => {
              this.form = res; 
        });
      },

      //单位
      initDW(){
        QueryAllChildDictViewByCode(this.Constant.C_HB).then((data)=>{
            this.HBTypeDatas = [];
            this.HBTypeDatas = data;
        });

        QueryComputeDirectionEnum().then((res)=>{
          this.computeDirectionDatas = [];
          this.computeDirectionDatas = res;
        });
      },

      /* 更新visible */
      updateVisible(value) {
        //更新父组件中的visible的值
        this.$emit('update:visible', value);
      },
     
    },
     
     
      /* 监听visible属性变化 */
      watch: {
        visible(visible) {
          if (visible) {
            if (this.data && this.data.id) {
              //赋值this.form
              this.form = {};
              this.setData(this.data.id);
              this.isUpdate = this.data.id !=null && this.data.id !=undefined;
            } else {
              this.form={};
              this.isUpdate = false;
            }
          } else {
              this.$refs['form'].clearValidate();
              this.form = {};
          }
        }
      }
    };
  </script>